In this session, we take the output table we’ve intended on creating in the second exercise in the session 23_analysis and we’re exploring the ways to link text and table content into one document - be it pdf, word of html.
You can have a look at the R Markdown basics here. This will help you in your first steps in editing text in Markdown documents.
For instance,
[text in document where the website is linked at](linking website here)If we knit this markdown document into a pdf or html document, we will only get the text and not the code as well. This is due to the fact that we’ve instructed RStudio to not echo the R chunk codes (see echo = FALSE).
If we now want to call R object that we’ve created in the chunks above, we would need to call them only in a separate R code chunk, as follows:
## # A tibble: 3 x 8
## model r_square res.df rss df sumsq statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 model 1 0.0505 3158 11784. NA NA NA NA
## 2 model 2 0.0512 3157 11772. 1 11.7 3.14 0.0764
## 3 model 3 0.0517 3156 11762. 1 10.1 2.72 0.0995
## package 'DT' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\stancian\AppData\Local\Temp\RtmpcZLXVV\downloaded_packages
This table doesn’t look too nice (in the output document) as it is. So, we can use the package kableExtra (or others) to edit it further, and make it more “presentable”.
| model | r_square | res.df | rss | df | sumsq | statistic | p.value |
|---|---|---|---|---|---|---|---|
| model 1 | 0.0505439 | 3158 | 11783.71 | NA | NA | NA | NA |
| model 2 | 0.0511868 | 3157 | 11772.00 | 1 | 11.70808 | 3.141563 | 0.0764171 |
| model 3 | 0.0517022 | 3156 | 11761.88 | 1 | 10.12110 | 2.715739 | 0.0994625 |
This already looks a bit better, but it is still far from perfect. Let us add a nother layer to it.
| model | r_square | res.df | rss | df | sumsq | statistic | p.value |
|---|---|---|---|---|---|---|---|
| model 1 | 0.0505439 | 3158 | 11783.71 | NA | NA | NA | NA |
| model 2 | 0.0511868 | 3157 | 11772.00 | 1 | 11.70808 | 3.141563 | 0.0764171 |
| model 3 | 0.0517022 | 3156 | 11761.88 | 1 | 10.12110 | 2.715739 | 0.0994625 |
We’ve applied a style layer called “classic”. We can also add more personalized styles. See here for some tips on how to do that with the package kableExtra or here for tips on using other packages.
Check the linked materials above, and add further styles to the table.
One additional strength of working with R and RStudio as an integrated work interface is that you can build data directly into the text, which has further applications when you write reports - you can use this feature to develop automatized reports (an example later on today).
Say, we are writing a report and we want to include in the text information that we’ve generated in the table of results above. We can do this with the so called in-line R code chunk, which works similarity to the regular chunk. See quotation below
Our analysis indicates that all three models have similar R2 values: model 1 = 0.0505439, model 2 = 0.0511868 and model 3 = 0.0517022.
Write a brief text presenting results using the tables of results above and when reporting a coefficient call that coefficient directly from the table - that is, do not write by hand but use in-line R code chunks.
…
One further feature of working in R is that you have almost infinite possibilities to how you present your data or findings.
In sessions 22_descriptives and 23_analysis we’ve first worked with package ggplot2 in creating varying plots for our data visualization.
We can do loads of creative things in presenting our results.
For instance, we can choose whether to display in one plot a factor variable as a grouping variable or whether to use that factor variable as a plot separation.
If you recall from before, we can add trend lines to each plot, as either a regression line or a simple trend. Let us add here a trend line (line is not estimated in a regression model).
BTW: The shadded cloud around the trend line signifies the 95% CI.
This already looks quite nice and particularly informative at a first glance. But, we can further improve on this plot by adding title and changing varying elements of the plot.
Already much better. But we can improve on this even further. For instance, we can add a specific theme to our plot. Either a pre-defined one, or we can define one the way we like it.
If for whatever reason you want to knit (export your Markdown document as an html file) you can even make your plots interactive using the package plotly.
Note: the interactive plots only make sense in html formats. They will not work in pdf or word formats.
In some cases we might rather prefer to save the plots as separate files and use them in your further reports or papers. We can easily do this, and specify several features when we export the file.
Use the information on the slides and customize a plot of your choice. Show to the rest of us what you’ve got! :)
Some ideas: